package puyu.manage.service.sample.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import puyu.manage.service.common.dao.CommonMapper;
import puyu.manage.service.file.entity.SysTFileUpload;
import puyu.manage.service.file.entity.vo.FileUploadVo;
import puyu.manage.service.file.service.SysTFileCenterService;
import puyu.manage.service.file.service.SysTFileUploadService;
import puyu.manage.service.sample.dao.SampleDao;
import puyu.manage.service.sample.service.SampleService;
import org.springframework.stereotype.Service;
import puyu.manage.service.utils.CommonUtil;
import puyu.manage.service.utils.IdUtils;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.*;

/**
 * 样品表(PuyuTSample)表服务实现类
 *
 * @author makejava
 * @since 2024-06-17 14:51:53
 */
@Service("SampleService")
@Transactional(rollbackFor = Exception.class)
public class SampleServiceImpl implements SampleService {
    @Resource
    private SampleDao sampleDao;

    @Autowired(required = false)
    CommonMapper commonMapper;

    @Resource
    private SysTFileUploadService sysTFileUploadService;

    @Resource
    private SysTFileCenterService sysTFileCenterService;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Map<String,Object> queryById(String id) {
        return this.sampleDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param puyuTSample 实例对象
     * @return 实例对象
     */
    @Override
    public Map<String,Object> insert(Map<String,Object> puyuTSample) {
        this.sampleDao.insert(puyuTSample);
        return null;
    }


    /**
     * 样品二维码生成
     *
     * @param puyuTSample 实例对象
     * @return 实例对象
     */
    @Override
    public Map<String,Object> editSampleCode(Map<String,Object> puyuTSample) throws Exception {


        String id =  puyuTSample.get("sampleId")+"";
        MultipartFile file =CommonUtil.imageConvert(puyuTSample.get("file")+"");
        //判空
        if (ObjectUtil.isNotEmpty(file)){

            //删除之前数据
            sysTFileCenterService.deleteByFile(id,"2","sample");

            //上传文件到Minio
            SysTFileUpload upload = sysTFileUploadService.upload(file);
            FileUploadVo vo = new FileUploadVo();
            vo.setId(id);
            List<String> ids = new ArrayList<>();
            ids.add(upload.getFileId());
            vo.setFileIds(ids);
            //文件关联表
            sysTFileCenterService.saveByFile(vo);
            puyuTSample=new HashMap<>();
            puyuTSample.put("sampleId",id);
            //二维码地址
            puyuTSample.put("sampleCodePath",upload.getFileUrl());
            //二维码生成时间
            puyuTSample.put("sampleCodeCreateTime", new Date());
//            //二维码生成状态
            puyuTSample.put("codeStatus", "2");
            //扫码次数默认0scan_num
            puyuTSample.put("scanNum", "0");
        }

        //修改样品状态为生成状态。二维码地址和时间
        this.sampleDao.update(puyuTSample);
        return null;
    }


    /**
     * 样品试验视频上传
     *
     * @param puyuTSample 实例对象
     * @return 实例对象
     */
    @Override
    public Map<String,Object> editSampleVideo(Map<String,Object> puyuTSample, MultipartFile file) throws Exception {


        String id = (String) puyuTSample.get("sampleId");

        //判空
        if (ObjectUtil.isNotEmpty(file)){
            //上传文件到Minio
            SysTFileUpload upload = sysTFileUploadService.upload(file);
            FileUploadVo vo = new FileUploadVo();
            vo.setId(id);
            List<String> ids = new ArrayList<>();
            ids.add(upload.getFileId());
            vo.setFileIds(ids);
            //文件关联表
            sysTFileCenterService.saveByFile(vo);

            //视频地址
            puyuTSample.put("videoPath",upload.getFileUrl());
        }

        //修改样品状态为生成状态。二维码地址和时间(视频状态、视频上传地址和开始结束时间)
        this.sampleDao.update(puyuTSample);
        return null;
    }

    /**
     * @Description 删除数据
     * @author whl
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String sampleId) {

        //删除文件和服务器文件
        String videoType = "1";
        sysTFileCenterService.deleteByFile(sampleId,videoType,"sample");

        //修改样品状态为生成状态。二维码地址和时间(视频状态、视频上传地址和开始结束时间)
        commonMapper.executeSql("update puyu_t_sample set video_path=null,video_status='1',video_start_time=null,video_end_time=null,video_content=null where sample_id ='"+sampleId+"'");

        return false;
    }


    /**
     * 修改数据
     *
     * @param puyuTSample 实例对象
     * @return 实例对象
     */
    @Override
    public Map<String,Object> updateList(Map<String,Object> puyuTSample, MultipartFile[] file) {


        List<Map<String,Object>> dataLists= (List<Map<String, Object>>) puyuTSample.get("datas");
//        String id = (String) puyuTSample.get("id");
//        //判空
//        if (ObjectUtil.isNotEmpty(file)){
//            //上传文件到Minio
//            SysTFileUpload upload = sysTFileUploadService.upload(file);
//            FileUploadVo vo = new FileUploadVo();
//            vo.setId(id);
//            List<String> ids = new ArrayList<>();
//            ids.add(upload.getFileId());
//            vo.setFileIds(ids);
//            //文件关联表
//            sysTFileCenterService.saveByFile(vo);
//
//            //二维码地址
//            puyuTSample.put("sampleCodePath",upload.getFileUrl());
//            //二维码生成时间
//            puyuTSample.put("sampleCodeCreateTime", new Date());
//        }

        //修改样品状态为生成状态。二维码地址和时间(视频状态、视频上传地址和开始结束时间)
        this.sampleDao.updateList(dataLists);
        return null;
    }

    @Override
    public String updateSampleNum(String id) {
        commonMapper.executeSql("update puyu_t_sample set scan_num=scan_num+1 where sample_id= '"+id+"'");
        return null;
    }
}
